package teacher

import (
	"anban/controllers"
	"anban/service"
	"anban/utils"
	"encoding/json"
	"fmt"
	"html/template"
	"net/url"
)

type IndexController struct {
	controllers.BaseController
}

func (c *IndexController) Index() {
	userId := c.GetSession("userId")

	p := map[string]interface{}{}
	p["userId"] = userId
	_, c.Data["classList"] = service.GetClassList(p)

	p = map[string]interface{}{}
	p["status"] = 1
	_, c.Data["courseList"] = service.GetCourseList(p)

	c.Data["title"] = "首页"
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "teacher/index.html"
}

func (c *IndexController) Register() {
	p := map[string]interface{}{}
	p["level"] = 1
	_, provinceList := service.GetRegionList(p)
	provinceValues := service.GetRegionListValues(provinceList)
	res, _ := json.Marshal(provinceValues)
	c.Data["provinceJson"] = string(res)

	p = map[string]interface{}{}
	p["status"] = 1
	_, courseList := service.GetCourseList(p)
	courseValues := service.GetCourseListValues(courseList)
	res, _ = json.Marshal(courseValues)
	c.Data["courseJson"] = string(res)

	c.Data["title"] = "教师注册"
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "teacher/register.html"
}

func (c *IndexController) DoRegister() {
	var res *utils.ResJsonStruct
	input := c.Input()

	userId := c.GetSession("userId")
	p := map[string]interface{}{}
	p["id"] = userId
	user := service.GetUserInfo(p)

	if user.Identity == 0 {
		if len(input["phone"][0]) <= 0 {
			res = utils.ResJson(0, "请输入手机号", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
		if !utils.IsMobile(input["phone"][0]) {
			res = utils.ResJson(0, "请输入正确的手机号", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
		verifyRes, verifyMsg := service.DoCheckVerifyCode(input["phone"][0], input["verify_code"][0])
		if !verifyRes {
			res = utils.ResJson(0, verifyMsg, "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
		if len(input["real_name"][0]) <= 0 {
			res = utils.ResJson(0, "请输入姓名", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	if len(input["province_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择省", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["city_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择市", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["district_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择区/县", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["school_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择学校", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["class_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择班级", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["course_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择科目", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	if input["is_main"][0] == "1" {
		p = map[string]interface{}{}
		p["teacherClassId"] = input["class_id"][0]
		_, classTeacherList := service.GetUserList(p)
		for _, classTeacher := range classTeacherList {
			if classTeacher.IsMain == 1 {
				res = utils.ResJson(0, "注册失败，这个班级已经有班主任了", "")
				c.Data["json"] = res
				c.ServeJSON()
				return
			}
		}
	}

	p = map[string]interface{}{}
	p["userId"] = user.Id
	p["classId"] = input["class_id"][0]
	userClass := service.GetUserClassInfo(p)
	if userClass.Id > 0 {
		res = utils.ResJson(0, "注册失败，您已经添加过这个班级了", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	input["user_id"] = []string{fmt.Sprintf("%d", user.Id)}
	_, err := service.AddUserClass(input)
	if err != nil {
		res = utils.ResJson(0, "注册失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	editInput := url.Values{
		"id": {fmt.Sprintf("%d", user.Id)},
		"is_main": {input["is_main"][0]},
	}
	if user.Identity == 1 {
		editInput["identity"] = []string{"3"}
	} else {
		editInput["identity"] = []string{"2"}
	}
	if _, ok := input["real_name"]; ok {
		realName := []rune(input["real_name"][0])
		editInput["real_name"] = []string{input["real_name"][0]}
		editInput["short_name"] = []string{fmt.Sprintf("%s老师", string(realName[:1]))}
	} else {
		realName := []rune(user.RealName)
		editInput["short_name"] = []string{fmt.Sprintf("%s老师", string(realName[:1]))}
	}
	if _, ok := input["phone"]; ok {
		editInput["phone"] = []string{input["phone"][0]}
	}
	service.EditUser(editInput)

	res = utils.ResJson(1, "注册成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}